'use strict';
module.exports = app => {
  const { STRING, INTEGER, TEXT } = app.Sequelize;
    const model = app.model.define(
      'post',
      {
          id: {type: INTEGER, primaryKey: true, autoIncrement: true},
          name: STRING(255),
          image: STRING(255),
          content: TEXT(),
          status: INTEGER(11),
          user_id: INTEGER(11),
          like_num: INTEGER(11),
          comment_num: INTEGER(11),
          collect_num: INTEGER(11),
      },
      {
          tableName: 'post',
          timestamps: true, // 启用时间戳(createdAt, updatedAt)
          updatedAt: 'updated_time', // 不想要 updatedAt
          createdAt: 'create_time', // 想要  createdAt 但是希望名称叫做 created_at
      }
    );
    // 关联关系
    model.associate = function() {
        // 一对一user
        model.belongsTo(app.model.User, { foreignKey: 'user_id', targetKey: 'id' });
        // 一对多个评论 comment
        model.hasMany(app.model.Comment, { foreignKey: 'post_id', targetKey: 'id' });
        model.hasMany(app.model.Collect, { foreignKey: 'post_id', targetKey: 'id' });
        model.hasMany(app.model.Like, { foreignKey: 'post_id', targetKey: 'id' });
    }
    return model
};
